package com.dynamixsoftware.printershare;

import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnClickListener;
import android.content.res.Resources;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.dynamixsoftware.printershare.data.Printer;
import com.dynamixsoftware.printershare.data.SoapEnvelope;
import com.dynamixsoftware.printershare.data.SoapService;
import com.dynamixsoftware.printershare.data.User;
import com.dynamixsoftware.printershare.data.XmlUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class ActivityFindPrinters extends ActivityCore {
	private static final int DIALOG_SEARCH = 1;
	private Vector<Printer> printers;
	private View view_dialog_search;
	private Thread wt;

	public void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		setContentView(2130903068);
		setTitle(2131099675);
		((Button) findViewById(2131230806)).setOnClickListener(new View.OnClickListener() {
			public void onClick(View paramAnonymousView) {
				ActivityFindPrinters.this.showDialog(1);
			}
		});
		ListView localListView = (ListView) findViewById(2131230805);
		localListView.setAdapter(new PrinterAdapter(this));
		localListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			public void onItemClick(AdapterView<?> paramAnonymousAdapterView, View paramAnonymousView,
					int paramAnonymousInt, long paramAnonymousLong) {
				Printer localPrinter = (Printer) paramAnonymousView.getTag();
				if (!ActivityCore.remote_printers.contains(localPrinter)) {
					ActivityFindPrinters.access$002(ActivityFindPrinters.this,
							new ActivityFindPrinters.AddPrinterThread(ActivityFindPrinters.this, localPrinter));
					ActivityFindPrinters.this.wt.start();
				}
			}
		});
		showDialog(1);
	}

	protected Dialog onCreateDialog(int paramInt) {
		LayoutInflater localLayoutInflater = LayoutInflater.from(this);
		switch (paramInt) {
		default:
			return null;
		}
		this.view_dialog_search = localLayoutInflater.inflate(2130903051, null);
		new AlertDialog.Builder(this).setIcon(2130837549).setTitle(2131099851).setView(this.view_dialog_search)
				.setOnCancelListener(new DialogInterface.OnCancelListener() {
					public void onCancel(DialogInterface paramAnonymousDialogInterface) {
						if (ActivityFindPrinters.this.printers == null) {
							ActivityFindPrinters.this.setResult(0);
							ActivityFindPrinters.this.finish();
						}
					}
				}).setPositiveButton(2131099723, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface paramAnonymousDialogInterface, int paramAnonymousInt) {
						EditText localEditText = (EditText) ActivityFindPrinters.this.view_dialog_search
								.findViewById(2131230765);
						CheckBox localCheckBox = (CheckBox) ActivityFindPrinters.this.view_dialog_search
								.findViewById(2131230760);
						ActivityFindPrinters.access$002(ActivityFindPrinters.this,
								new ActivityFindPrinters.FindPrinterThread(ActivityFindPrinters.this, localEditText
										.getText().toString(), localCheckBox.isChecked()));
						ActivityFindPrinters.this.wt.start();
					}
				}).setNegativeButton(2131099707, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface paramAnonymousDialogInterface, int paramAnonymousInt) {
						paramAnonymousDialogInterface.cancel();
					}
				}).create();
	}

	class AddPrinterThread extends Thread {
		private Printer prn;

		public AddPrinterThread(Printer paramPrinter) {
			this.prn = paramPrinter;
		}

		public void run() {
			ActivityFindPrinters.this.runOnUiThread(new Runnable() {
				public void run() {
					ActivityFindPrinters.this.showProgress(ActivityFindPrinters.this.getResources().getString(
							2131099746));
				}
			});
			ActivityFindPrinters.this.last_error = null;
			try {
				SoapEnvelope localSoapEnvelope = new SoapEnvelope("ChangePrinters", "Param", "data");
				Element localElement1 = localSoapEnvelope.getDataRoot();
				XmlUtil.appendElement(localElement1, "token", ActivityCore.token);
				localElement1.setAttribute("action", "like");
				XmlUtil.appendElement(XmlUtil.appendElement(localElement1, "printer"), "public-id", this.prn.id);
				Element localElement2 = ActivityFindPrinters.this.psService.doAction(localSoapEnvelope).getDataRoot();
				if ("true".equals(localElement2.getAttribute("success"))) {
					this.prn = new Printer();
					this.prn.readFromXml(XmlUtil.getFirstElement(localElement2, "printer"));
					ActivityCore.remote_printers.add(this.prn);
					if (ActivityCore.printer == null) {
						ActivityFindPrinters.this.setPrinter(this.prn);
					}
				}
				while (ActivityFindPrinters.this.last_error == null) {
					ActivityFindPrinters.this.runOnUiThread(new Runnable() {
						public void run() {
							ActivityFindPrinters.this.hideProgress();
							if (ActivityCore.remote_printers.size() == 1) {
								ActivityFindPrinters.this.setResult(-1);
							}
							ActivityFindPrinters.this.finish();
						}
					});
					ActivityFindPrinters.access$002(ActivityFindPrinters.this, null);
					return;
					ActivityFindPrinters.this.last_error = ("Error: " + XmlUtil.getFirstNodeValue(localElement2,
							"message"));
				}
			} catch (Exception localException) {
				for (;;) {
					localException.printStackTrace();
					ActivityFindPrinters.this.last_error = ("Internal Error: " + localException.getMessage());
					App.reportThrowable(localException);
					continue;
					ActivityFindPrinters.this.runOnUiThread(new Runnable() {
						public void run() {
							ActivityFindPrinters.this.hideProgress();
							ActivityFindPrinters.this.displayLastError(new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface paramAnonymous2DialogInterface,
										int paramAnonymous2Int) {
								}
							});
						}
					});
				}
			}
		}
	}

	class FindPrinterThread extends Thread {
		String keyword;
		boolean online_only;

		public FindPrinterThread(String paramString, boolean paramBoolean) {
			this.keyword = paramString;
			this.online_only = paramBoolean;
		}

		public void run() {
			ActivityFindPrinters.this.runOnUiThread(new Runnable() {
				public void run() {
					ActivityFindPrinters.this.showProgress(ActivityFindPrinters.this.getResources().getString(
							2131099755));
				}
			});
			ActivityFindPrinters.this.last_error = null;
			if (ActivityFindPrinters.this.printers == null) {
				ActivityFindPrinters.access$102(ActivityFindPrinters.this, new Vector());
			}
			ActivityFindPrinters.this.printers.clear();
			try {
				SoapEnvelope localSoapEnvelope = new SoapEnvelope("SearchPrinters", "Param", "data");
				Element localElement1 = localSoapEnvelope.getDataRoot();
				XmlUtil.appendElement(localElement1, "token", ActivityCore.token);
				localElement1.setAttribute("page-size", "50");
				localElement1.setAttribute("echo", "mobile");
				localElement1.setAttribute("real", "true");
				if (this.online_only) {
					localElement1.setAttribute("online", "true");
				}
				XmlUtil.appendElement(localElement1, "keyword", this.keyword);
				Element localElement2 = ActivityFindPrinters.this.psService.doAction(localSoapEnvelope).getDataRoot();
				if ("true".equals(localElement2.getAttribute("success"))) {
					NodeList localNodeList = localElement2.getElementsByTagName("printer");
					int i = localNodeList.getLength();
					for (int j = 0; j < i; j++) {
						Printer localPrinter = new Printer();
						localPrinter.readFromXml((Element) localNodeList.item(j));
						ActivityFindPrinters.this.printers.add(localPrinter);
					}
				}
				ActivityFindPrinters.this.last_error = ("Error: " + XmlUtil.getFirstNodeValue(localElement2, "message"));
			} catch (Exception localException) {
				for (;;) {
					localException.printStackTrace();
					ActivityFindPrinters.this.last_error = ("Internal Error: " + localException.getMessage());
					App.reportThrowable(localException);
				}
			}
			ActivityFindPrinters.this.runOnUiThread(new Runnable() {
				public void run() {
					ActivityFindPrinters.this.hideProgress();
					TextView localTextView = (TextView) ActivityFindPrinters.this.findViewById(2131230804);
					String str2;
					Object[] arrayOfObject;
					Object localObject;
					if (ActivityFindPrinters.this.printers != null) {
						str2 = ActivityFindPrinters.this.getResources().getString(2131099753);
						arrayOfObject = new Object[1];
						if (ActivityFindPrinters.this.printers.size() >= 50) {
							localObject = String.format(ActivityFindPrinters.this.getResources().getString(2131099756),
									new Object[] { "50" });
							arrayOfObject[0] = localObject;
						}
					}
					for (String str1 = String.format(str2, arrayOfObject);; str1 = "") {
						localTextView.setText(str1);
						((ActivityFindPrinters.PrinterAdapter) ((ListView) ActivityFindPrinters.this
								.findViewById(2131230805)).getAdapter()).fireOnChanged();
						if (ActivityFindPrinters.this.last_error != null) {
							ActivityFindPrinters.this.displayLastError(new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface paramAnonymous2DialogInterface,
										int paramAnonymous2Int) {
								}
							});
						}
						return;
						localObject = Integer.valueOf(ActivityFindPrinters.this.printers.size());
						break;
					}
				}
			});
			ActivityFindPrinters.access$002(ActivityFindPrinters.this, null);
		}
	}

	class PrinterAdapter implements ListAdapter {
		public Context mContext;
		private List<DataSetObserver> observers = null;

		public PrinterAdapter(Context paramContext) {
			this.mContext = paramContext;
		}

		public boolean areAllItemsEnabled() {
			return true;
		}

		public void fireOnChanged() {
			if (this.observers != null) {
				int i = this.observers.size();
				for (int j = 0; j < i; j++) {
					((DataSetObserver) this.observers.get(j)).onChanged();
				}
			}
		}

		public int getCount() {
			if (ActivityFindPrinters.this.printers != null) {
				return ActivityFindPrinters.this.printers.size();
			}
			return 0;
		}

		public Object getItem(int paramInt) {
			return ActivityFindPrinters.this.printers.elementAt(paramInt);
		}

		public long getItemId(int paramInt) {
			return paramInt;
		}

		public int getItemViewType(int paramInt) {
			return 1;
		}

		public View getView(int paramInt, View paramView, ViewGroup paramViewGroup)
    {
      View localView1 = LayoutInflater.from(this.mContext).inflate(2130903064, null);
      Printer localPrinter1 = (Printer)ActivityFindPrinters.this.printers.elementAt(paramInt);
      TextView localTextView1 = (TextView)localView1.findViewById(2131230800);
      String str1 = "[" + localPrinter1.owner.login + "]";
      if ((localPrinter1.owner.nick != null) && (localPrinter1.owner.nick.length() > 0)) {
        str1 = localPrinter1.owner.nick;
      }
      if ((localPrinter1.owner.name != null) && (localPrinter1.owner.name.length() > 0)) {
        str1 = localPrinter1.owner.name;
      }
      localTextView1.setText(str1);
      TextView localTextView2 = (TextView)localView1.findViewById(2131230758);
      String str2;
      String str3;
      String str5;
      label300:
      label317:
      int i;
      label370:
      int j;
      label401:
      View localView2;
      int k;
      if ((localPrinter1 != null) && (localPrinter1.title != null))
      {
        str2 = localPrinter1.title;
        localTextView2.setText(str2);
        TextView localTextView3 = (TextView)localView1.findViewById(2131230759);
        str3 = "";
        if ((localPrinter1.owner.country != null) && (localPrinter1.owner.country.length() > 0)) {
          str3 = localPrinter1.owner.country;
        }
        if ((localPrinter1.owner.city == null) || (localPrinter1.owner.city.length() <= 0)) {
          break label463;
        }
        StringBuilder localStringBuilder2 = new StringBuilder().append(localPrinter1.owner.city);
        if (str3.length() <= 0) {
          break label456;
        }
        str5 = ", ";
        str3 = str5 + str3;
        if ((localPrinter1.location != null) && (localPrinter1.location.length() > 0)) {
          str3 = localPrinter1.location;
        }
        if (str3 == null) {
          str3 = "";
        }
        localTextView3.setText(str3);
        if (str3.length() <= 0) {
          break label547;
        }
        i = 0;
        localTextView3.setVisibility(i);
        ImageView localImageView = (ImageView)localView1.findViewById(2131230760);
        if (!localPrinter1.online) {
          break label554;
        }
        j = 2130837561;
        localImageView.setBackgroundResource(j);
        localView2 = localView1.findViewById(2131230801);
        Printer localPrinter2 = ActivityCore.printer;
        k = 0;
        if (localPrinter1 != localPrinter2) {
          break label561;
        }
      }
      for (;;)
      {
        localView2.setVisibility(k);
        localView1.setTag(localPrinter1);
        return localView1;
        str2 = "";
        break;
        label456:
        str5 = "";
        break label300;
        label463:
        if ((localPrinter1.owner.state == null) || (localPrinter1.owner.state.length() <= 0)) {
          break label317;
        }
        StringBuilder localStringBuilder1 = new StringBuilder().append(localPrinter1.owner.state);
        if (str3.length() > 0) {}
        for (String str4 = ", ";; str4 = "")
        {
          str3 = str4 + str3;
          break;
        }
        label547:
        i = 8;
        break label370;
        label554:
        j = 2130837560;
        break label401;
        label561:
        k = 4;
      }
    }

		public int getViewTypeCount() {
			return 1;
		}

		public boolean hasStableIds() {
			return false;
		}

		public boolean isEmpty() {
			return (ActivityFindPrinters.this.printers == null) || (ActivityFindPrinters.this.printers.size() == 0);
		}

		public boolean isEnabled(int paramInt) {
			return true;
		}

		public void registerDataSetObserver(DataSetObserver paramDataSetObserver) {
			if (this.observers == null) {
				this.observers = new ArrayList();
			}
			if (!this.observers.contains(paramDataSetObserver)) {
				this.observers.add(paramDataSetObserver);
			}
		}

		public void unregisterDataSetObserver(DataSetObserver paramDataSetObserver) {
			if (this.observers == null) {
				return;
			}
			this.observers.remove(paramDataSetObserver);
		}
	}
}
